/**
 * 
 */
package ro.utcn.tp.taskManagement.persistence;

import java.util.Calendar;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import ro.utcn.tp.taskManagement.model.Task;
import ro.utcn.tp.taskManagement.utils.DBUtils;
import ro.utcn.tp.taskManagement.utils.DateUtils;

/**
 * @author Coni
 * 
 */
public class TasksPersistence {

	public void saveTask(Task task) throws Exception {
		System.out.println("saveTask(" + task.getId() + ")");
		Transaction t = null;
		Session session = PersistenceFactory.getInstance().getSession();
		try {
			t = session.beginTransaction();

			if (task.getId() == 0) {
				session.save(task);
			} else {
				session.update(task);
			}
			t.commit();
		} catch (HibernateException hibernateException) {
			hibernateException.printStackTrace();
			DBUtils.rollback(t);
			throw new Exception(hibernateException);
		}
	}

	public List<Task> getUserTasks(int userId) throws Exception {
		System.out.println("getUserTasks(" + userId + ")");
		Transaction t = null;
		Session session = PersistenceFactory.getInstance().getSession();
		try {
			t = session.beginTransaction();
			Criteria criteria = session.createCriteria(Task.class);
			criteria = criteria.add(Restrictions.eq("userId", userId));
			List<Task> tasks = (List<Task>) criteria.list();
			t.commit();
			return tasks;
		} catch (HibernateException hibernateException) {
			hibernateException.printStackTrace();
			DBUtils.rollback(t);
			throw new Exception(hibernateException);
		}
	}

	public void deleteTask(Task task) throws Exception {
		System.out.println("deleteTask(" + task.getId() + ")");
		Transaction t = null;
		Session session = PersistenceFactory.getInstance().getSession();
		try {
			t = session.beginTransaction();
			session.delete(task);
			t.commit();
		} catch (HibernateException hibernateException) {
			hibernateException.printStackTrace();
			DBUtils.rollback(t);
			throw new Exception(hibernateException);
		}
	}

	public List<Task> getTodayTasks(int userId, Calendar selectedDate)
			throws Exception {
		System.out.println("getUserTasks(" + userId + ")");
		Transaction t = null;
		Session session = PersistenceFactory.getInstance().getSession();
		try {
			t = session.beginTransaction();
			Criteria criteria = session.createCriteria(Task.class);
			criteria = criteria.add(Restrictions.eq("userId", userId));
			criteria = criteria.add(Restrictions.le("scheduleDate", DateUtils.getTodayDate()));
			criteria = criteria.add(Restrictions.or(
					Restrictions.isNull("closedDate"),
					Restrictions.eq("closedDate", selectedDate)));
			List<Task> tasks = (List<Task>) criteria.list();
			t.commit();
			return tasks;
		} catch (HibernateException hibernateException) {
			hibernateException.printStackTrace();
			DBUtils.rollback(t);
			throw new Exception(hibernateException);
		}
	}

	public List<Task> getPastDayTasks(int userId, Calendar selectedDate) throws Exception {
		System.out.println("getUserTasks(" + userId + ")");
		Transaction t = null;
		Session session = PersistenceFactory.getInstance().getSession();
		try {
			t = session.beginTransaction();
			Criteria criteria = session.createCriteria(Task.class);
			criteria = criteria.add(Restrictions.eq("userId", userId));
			criteria = criteria.add(Restrictions.eq("closedDate", selectedDate));
			List<Task> tasks = (List<Task>) criteria.list();
			t.commit();
			return tasks;
		} catch (HibernateException hibernateException) {
			hibernateException.printStackTrace();
			DBUtils.rollback(t);
			throw new Exception(hibernateException);
		}
	}

	public List<Task> getFutureDayTasks(int userId, Calendar selectedDate) throws Exception{
		System.out.println("getUserTasks(" + userId + ")");
		Transaction t = null;
		Session session = PersistenceFactory.getInstance().getSession();
		try {
			t = session.beginTransaction();
			Criteria criteria = session.createCriteria(Task.class);
			criteria = criteria.add(Restrictions.eq("userId", userId));
			criteria = criteria.add(Restrictions.eq("scheduleDate", selectedDate));
			List<Task> tasks = (List<Task>) criteria.list();
			t.commit();
			return tasks;
		} catch (HibernateException hibernateException) {
			hibernateException.printStackTrace();
			DBUtils.rollback(t);
			throw new Exception(hibernateException);
		}
	}
}
